/**
 * vue常用函数工具类
 * @author:chenyi
 * @version 1.0
 * @Date: 2019/05/22
 */
"use strict";
let utils = {
    /**
     * 获取前端缓存
     * @Date: 2019/05/22
     */
    getLocalStorageItem: function (key) {
        return JSON.parse(localStorage.getItem(key));
    },
    /**
     * 设置前端缓存
     * @Date: 2019/05/22
     */
    setLocalStorageItem: function (key, data) {
        localStorage.setItem(key, JSON.stringify(data));
    },
    /**
     * url以http[s]://开头
     * */
    isURL: function (url) {
        let Expression = /http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/;
        let objExp = new RegExp(Expression);
        return objExp.test(url) === true;
    },
    /**通过url获取数据*/
    getDataByUrl: function (url) {
        let data;
        $.ajax({
            url: url,
            async: false,
            type: 'post',
            dataType: "json",
            success: function (res) {
                if (res.code === 0) {
                    data = res.data;
                } else {
                    data = {};
                    top.app.$message.error(res.msg);
                }
            }
        });
        return data;
    },
    getEnums: function (enumName) {
        let _this = this;
        /**localStorage是否已存在该数据*/
        let data = this.getLocalStorageItem(enumName);
        if (!data) {
            $.ajax({
                url: $s.getDataByEnum,
                type: 'post',
                async: false,
                data: {enumName: enumName},
                dataType: "json",
                success: function (res) {
                    if (res.code === 0) {
                        data = res.data;
                        /**设置localStorage缓存*/
                        _this.setLocalStorageItem(enumName, data);
                    } else {
                        data = {};
                        top.app.$message.error(res.msg);
                    }
                }
            });
        }
        return data;
    },
    /**通过字典获取数据*/
    getCodes: function (codeName) {
        let _this = this;
        /**localStorage是否已存在该数据*/
        let data = this.getLocalStorageItem(codeName);
        if (!data) {
            $.ajax({
                url: $s.getDataByCode,
                data: {codeName: codeName},
                async: false,
                type: 'post',
                dataType: "json",
                success: function (res) {
                    if (res.code === 0) {
                        data = res.data;
                        /**设置localStorage缓存*/
                        _this.setLocalStorageItem(codeName, data);
                    } else {
                        data = {};
                        top.app.$message.error(res.msg);
                    }
                }
            });

        }

        return data;
    },
    /**
     * 字典转义
     *@param codeName 字典key
     *@params code 要转义的字典值
     */
    codeFormat: function (codeName, code) {
        let _this = this;
        /**localStorage是否已存在该数据*/
        let data = _this.getLocalStorageItem(codeName);
        if (!data || data.length === 0) {
            $.ajax({
                url: $s.getDataByCode,
                async: false,
                type: 'post',
                data: {codeName: codeName},
                dataType: "json",
                success: function (res) {
                    if (res.code === 0) {
                        data = res.data;
                        /**设置localStorage缓存*/
                        _this.setLocalStorageItem(codeName, data);
                    } else {
                        data = {};
                        top.app.$message.error(res.msg);
                    }
                }
            });
        }
        let value = "";
        for (let item in data) {
            if (data[item].code == code) {
                value = data[item].value;
                break;
            }
        }
        return value;
    }
    ,
    /**
     * 枚举转义
     *@param enumName 枚举key
     *@params code 要转义的枚举值
     */
    enumFormat: function (enumName, code) {
        let _this = this;
        /**localStorage是否已存在该数据*/
        let data = _this.getLocalStorageItem(enumName);
        if (!data || data.length === 0) {
            $.ajax({
                url: $s.getDataByEnum,
                async: false,
                type: 'post',
                data: {enumName: enumName},
                dataType: "json",
                success: function (res) {
                    if (res.code === 0) {
                        data = res.data;
                        /**设置localStorage缓存*/
                        _this.setLocalStorageItem(enumName, data);
                    } else {
                        data = {};
                        top.app.$message.error(res.msg);
                    }
                }
            });
        }
        let value = "";
        for (let item in data) {
            if (data[item].code == code) {
                value = data[item].value;
                break;
            }
        }
        return value;
    },
    /**
     * 通过url转义
     *@param url 数据源地址
     *@params code 要转义的枚举值
     */
    urlFormat: function (url, code) {
        let data = [];
        $.ajax({
            url: url,
            async: false,
            type: 'post',
            dataType: "json",
            success: function (res) {
                if (res.code === 0) {
                    data = res.data;
                } else {
                    top.app.$message.error(res.msg);
                }
            }
        });
        let value = "";
        for (let item in data) {
            if (data[item].code == code) {
                value = data[item].value;
                break;
            }
        }
        return value;
    },
    /**
     * 颜色渲染
     *@param index 颜色数组下标
     *@params value 要渲染的文本
     */
    renderColor: function (index, value) {
        let color = $s.colors[index];
        return '<span style="color:' + color + '">' + value + '</span>'
    },
    /**
     * 产生一个唯一的uuid
     * @param len 产生的字符串长度
     * @param radix 进制数
     */
    getUUID: function (len, radix) {
        let chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
        let uuid = [], i;
        radix = radix || chars.length;
        if (len) {
            for (i = 0; i < len; i++) {
                uuid[i] = chars[0 | Math.random() * radix];
            }
        } else {
            let r;
            uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
            uuid[14] = '4';
            for (i = 0; i < 36; i++) {
                if (!uuid[i]) {
                    r = 0 | Math.random() * 16;
                    uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
                }
            }
        }
        return uuid.join('');
    },
    /**
     * 日期格式化
     * @param fmt   转化格式
     * @param dateStr   时间
     * @Date: 2017/12/8
     */
    dateFormat: function (dateStr, fmt) {
        if (!dateStr) {
            return ""
        }
        let date = new Date(dateStr);
        let o = {
            "M+": date.getMonth() + 1,                 //月份
            "d+": date.getDate(),                    //日
            "h+": date.getHours(),                   //小时
            "m+": date.getMinutes(),                 //分
            "s+": date.getSeconds(),                 //秒
            "q+": Math.floor((date.getMonth() + 3) / 3), //季度
            "S": date.getMilliseconds()             //毫秒
        };
        if (/(y+)/.test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
        }
        for (let k in o) {
            if (new RegExp("(" + k + ")").test(fmt)) {
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
            }
        }
        return fmt;
    },

    /**
     * 时间格式化
     * @param fmt        转化格式
     * @param timeStamp   时间戳
     * @Date: 2017/12/8
     */
    timeFormat: function (timeStamp, fmt) {
        if (!timeStamp) {
            return ""
        }
        let date = new Date();
        if (timeStamp.length === 10) {
            date.setTime(timeStamp * 1000);
        }
        let y = date.getFullYear();
        let m = date.getMonth() + 1;
        m = m < 10 ? ('0' + m) : m;
        let d = date.getDate();
        d = d < 10 ? ('0' + d) : d;
        let h = date.getHours();
        h = h < 10 ? ('0' + h) : h;
        let minute = date.getMinutes();
        let second = date.getSeconds();
        minute = minute < 10 ? ('0' + minute) : minute;
        second = second < 10 ? ('0' + second) : second;
        return $t.dateFormat(y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second, fmt);
    },
};
export default utils;
